var app = getApp();
import { request } from '../../request/index.js';
var util = require('../../utils/util.js');
var app = getApp();
var icon = true;
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		navbar: ['供应', '求购'],
		currentTab: 0,
		currentTab1: 0,
		homePic: [],
		pageNum: 1,
		pageNum1: 1,
		total: '',
		total1: '',
		goodResources: [],
		goodManufacturer: [],
		user: {},
		background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
		indicatorDots: true,
		vertical: false,
		autoplay: true,
		interval: 2000,
		duration: 500,
		bannerHeight: 200, //banner高度
		scrollTop: 0, //滚动条高度
	},
	call(e) {
		var time = this.data.user.memberDueDate;
		var dd = util.formatDate(new Date());
		var that = this;
		var phone = e.currentTarget.dataset.ph;
		var userId = app.globalData.userId;
		var userIdByResource = e.currentTarget.dataset.u;
		var resourcesId = e.currentTarget.dataset.r;
		if (this.data.user.userType == 1) {
			wx.showToast({
				title: '您不是会员，请充值查看',
				icon: 'none',
				duration: 1500,
			});
			setTimeout(function () {
				wx.navigateTo({
					url:
						'/pages/member/member?userId=' +
						userId +
						'&userIdByResource=' +
						userIdByResource,
				});
			}, 1500);
			return;
		}
		if (!time) {
			wx.showToast({
				title: '您不是会员，请充值查看',
				icon: 'none',
				duration: 1500,
			});
			setTimeout(function () {
				wx.navigateTo({
					url:
						'/pages/member/member?userId=' +
						userId +
						'&userIdByResource=' +
						userIdByResource,
				});
			}, 1500);
		} else {
			if (dd > time) {
				wx.showToast({
					title: '您的会员已到期，请充值查看',
					icon: 'none',
					duration: 1500,
				});
				setTimeout(function () {
					wx.navigateTo({
						url:
							'/pages/member/member?userId=' +
							userId +
							'&userIdByResource=' +
							userIdByResource,
					});
				}, 1500);
			} else {
				request({
					url: '/UserInfo/getUserViewPhoneLogByUserId',
					data: {
						userId: userId,
					},
				}).then((res) => {
					var flag = false;

					for (var i = 0; i < res.data.length; i++) {
						if (res.data[i].resourcesId == resourcesId) {
							flag = true;
						}
					}
					if (that.data.user.userType == 0) {
						flag = true;
					}
					if (flag == true) {
						wx.showModal({
							title: '电话',
							content: phone,
							success(res) {
								if (res.confirm) {
									wx.makePhoneCall({
										phoneNumber: phone,
									});
								} else if (res.cancel) {
								}
							},
						});
					} else if (
						res.data.length < that.data.user.viewPhones &&
						flag == false
					) {
						request({
							url: '/UserInfo/addUserViewPhoneLog',
							data: {
								userId: userId,
								resourcesId: resourcesId,
								phone: phone,
							},
						});
						wx.showModal({
							title: '电话',
							content: phone,
							success(res) {
								if (res.confirm) {
									wx.makePhoneCall({
										phoneNumber: phone,
									});
								} else if (res.cancel) {
								}
							},
						});
					} else {
						wx.showToast({
							title: '您今日的查看次数已达上限',
							icon: 'none',
							duration: 1500,
						});
					}
				});
			}
		}
	},

	//点击导航
	navbarTap: function (e) {
		// console.log(e.currentTarget.dataset.index)
		let index = e.currentTarget.dataset.index;
		this.setData({
			currentTab: index,
		});
	},

	//首页和搜索的切换
	navbarTap1: function (e) {
		// console.log(e.currentTarget.dataset.index)
		let index = e.currentTarget.dataset.idx;
		this.setData({
			currentTab1: index,
		});
		this.getGoodMfList1();
	},

	getGoodMfList1() {
		var that = this;
		if (this.data.currentTab1 == 0) {
			wx.showLoading({
				title: '加载中',
			});
			request({
				url: '/Resources/getAllResources',
				data: {
					pageNum: 1,
					resourcesType: 10,
					isFrontShow: 1,
				},
			}).then((r) => {
				that.setData({
					goodManufacturer: r.data.list,
					goodResources: [],
					total: r.data.total,
					pageNum: 1,
				});
				wx.hideLoading();
			});
		} else if (this.data.currentTab1 == 1) {
			wx.showLoading({
				title: '加载中',
			});
			request({
				url: '/Resources/getAllResources',
				data: {
					resourcesType: 3,
					isFrontShow: 1,
					pageNum: 1,
				},
			}).then((res) => {
				that.setData({
					goodResources: res.data.list,
					total1: res.data.total,
					pageNum1: 1,
				});
				wx.hideLoading();
			});
		}
	},
	getGoodMfList: function () {
		var that = this;
		if (that.data.currentTab1 == 0) {
			wx.showLoading({
				title: '加载中',
			});
			request({
				url: '/Resources/getAllResources',
				data: {
					pageNum: that.data.pageNum,
					resourcesType: 10,
					isFrontShow: 1,
				},
			}).then((res) => {
				var list1 = that.data.goodManufacturer;
				var list2 = res.data.list;
				if (that.data.pageNum === 1) list1 = list2;
				else list1 = list1.concat(list2);
				that.setData({
					goodManufacturer: list1,
					total: res.data.total,
				});
				wx.hideLoading();
			});
		} else if (that.data.currentTab1 == 1) {
			wx.showLoading({
				title: '加载中',
			});
			request({
				url: '/Resources/getAllResources',
				data: {
					resourcesType: 3,
					isFrontShow: 1,
					pageNum: that.data.pageNum1,
				},
			}).then((res) => {
				var list1 = that.data.goodResources;
				var list2 = res.data.list;
				if (that.data.pageNum1 === 1) list1 = list2;
				else list1 = list1.concat(list2);
				that.setData({
					goodResources: list1,
					total1: res.data.total,
				});
				wx.hideLoading();
			});
		}
	},

	onLoad: function (options) {
		if (options.isshare == 1) {
			wx.navigateTo({
				url: '../guang/guang',
			});
		}
		var userId = app.globalData.userId;
		request({
			url: '/Advertisement/getAdvertisementId',
		}).then((res) => {
			this.setData({
				homePic: res.data,
			});
		});
		this.getGoodMfList();
		request({
			url: '/UserInfo/getUserInfo',
			data: {
				userId: userId,
			},
		}).then((re) => {
			this.setData({
				user: re.data,
			});
		});

		wx.createSelectorQuery()
			.select('.banner')
			.boundingClientRect((rect) => {
				this.setData({
					bannerHeight: rect.height,
				});
			})
			.exec();
	},

	onPageScroll: function (e) {
		this.setData({
			scrollTop: e.scrollTop,
		});
	},

	onTouch: function (event) {
		wx.switchTab({
			url: '../xiangqingye/xiangqingye',
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getGoodMfList();
		this.getGoodMfList1();
	},

	onPageScroll: function (e) {
		this.setData({
			scrollTop: e.scrollTop,
		});
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		this.setData({
			pageNum: 1,
			pageNum1: 1,
		});
		this.getGoodMfList();
		this.getGoodMfList1();
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		var that = this;
		if (that.data.currentTab1 != 1) {
			if (that.data.pageNum * 10 < that.data.total) {
				var pageNum = that.data.pageNum + 1;
				that.setData({
					pageNum: pageNum,
				});
				that.getGoodMfList();
			} else {
				wx.showToast({
					title: '已经到底了',
					icon: 'none',
					duration: 1000,
				});
			}
		} else if (that.data.currentTab1 != 0) {
			if (that.data.pageNum1 * 10 < that.data.total1) {
				var pageNum1 = that.data.pageNum1 + 1;
				that.setData({
					pageNum1: pageNum1,
				});
				that.getGoodMfList();
			} else {
				wx.showToast({
					title: '已经到底了',
					icon: 'none',
					duration: 1000,
				});
			}
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		return {
			path: '/pages/supply/supply?isshare=1',
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			},
		};
	},
});
